'''
Created on Aug 29, 2013

@author: marchi
'''
import numpy as np
import Smooth as sm

def doit(filein,fileout='Profile1D.dat',nstep=2):
    fb=open(filein,'r')
    fout=open(fileout,'w')
    Lines=fb.readlines()
    rng=Lines[0].replace(':',' ').replace('[',' ').replace(']',' ').replace('\n',' ').split()
    Range=np.arange(float(rng[0]),float(rng[2])+float(rng[1]),float(rng[1]))
    Dim=Lines[1].replace('\n',' ').split()
    for o in range(3):
        Dim[o]=float(Dim[o])
    LinesS=Lines[2].split()
    for o in range(len(LinesS)):
        LinesS[o]=float(LinesS[o])
    
    Z0=np.reshape(LinesS,Dim)
    Z=sm.blur_image(Z0,3)
    print max(LinesS)
    Dim0=Z.shape
    DimX=(Dim0[0]-1)/2
    DimY=(Dim0[1]-1)/2
    DimZ=(Dim0[2]-1)/2
    dx=float(rng[2])/float(DimX)
    Range=np.arange(float(rng[0]),float(rng[2])+dx,dx)
    Range.shape
    
    Bx=np.zeros(Dim0[0])
    By=np.zeros(Dim0[1])
    Bz=np.zeros(Dim0[2])
    print DimX,DimY,DimZ
    print Bx.shape,By.shape,Bz.shape
    n=0
    for o in range(-nstep,nstep+1):
        for p in range(-nstep,nstep+1):
            Bx+=Z[:,DimY+o,DimZ+p]
            By+=Z[DimX+o,:,DimZ+p]
            Bz+=Z[DimX+o,DimY+p,:]
            n+=1
    Bx=Bx/n;By=By/n;Bz=Bz/n;
    for o in range(len(Bz)):
        str=' %9.3f  %14.8f %14.8f %14.8f \n' % (Range[o],Bx[o],By[o],Bz[o])
        fout.write(str)
    print Z.shape
    return Z

    

